import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:flutter_wanandroid/NetUtil.dart';
import 'package:flutter_wanandroid/Routes.dart';
import 'package:flutter_wanandroid/login_bean.dart';
import 'package:toast/toast.dart';

class Login extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return LoginState();
  }
}

class LoginState extends State<Login> {
  TextEditingController _userTextEditingController = TextEditingController();
  TextEditingController _pwdTextEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('登录'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            TextField(
              controller: _userTextEditingController,
              decoration: InputDecoration(
                fillColor: Colors.white,
                filled: true,
                labelText: 'username',
              ),
            ),
            TextField(
              controller: _pwdTextEditingController,
              decoration: InputDecoration(
                fillColor: Colors.white,
                filled: true,
                labelText: 'password',
              ),
            ),
            InkWell(
              onTap: () {
                Map<String, String> map = HashMap();
                map["username"] = _userTextEditingController.text;
                map["password"] = _pwdTextEditingController.text;
                OkhttpUtil.post("https://www.wanandroid.com/user/login",
                    (date) {
                  var loginBean = LoginBean.fromJson(date);
                  if (loginBean.errorCode == -1) {
                    Toast.show(loginBean.errorMsg, context,
                        duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
                  } else if (loginBean.errorCode == 0) {
                    Routes.router.navigateTo(context, '${Routes.home}');
                  }
                }, params: map);
              },
              child: Container(
                margin: const EdgeInsets.only(top: 40.0),
                height: 60,
                width: 200,
                child: Center(
                  child: Text(
                    '登录',
                    style: TextStyle(
                      fontSize: 20.0,
                      color: Colors.white,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ),
                decoration: BoxDecoration(
                  color: Colors.blue.withOpacity(0.5),
                  borderRadius: BorderRadius.circular(10.0),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
